var report1, report2;

$(function() {
 	report1 = echarts.init(document.getElementById('report1'));
	changeDateType();
	$("#profitPanel").panel({
		onResize : resizeEchartReport
	});
});

function changeDateType(){
	var checked = $("#analysisDateType").switchbutton("options").checked;
	if(checked){
		$("#dateTypeLong").show();
		$("#dateTypeRange").hide();
	} else {
		$("#dateTypeLong").hide();
		$("#dateTypeRange").show();
	}
}

var lastWidth = 0;
function resizeEchartReport() {
	var containerWidth = $("#profitPanel").width();
	var width = containerWidth;
	if(containerWidth>1000){
		width = 1000;
	}else if(containerWidth<600){
		width = 600;
	}
	if(lastWidth == width){
		return;
	}
	var height = width/2;
	$("#report1").width(width);
	$("#report1").height(height);
	var myChart1 = echarts.getInstanceByDom(document.getElementById('report1'));
	if(myChart1) myChart1.resize();
};

var sec_select_whitch;
function click_add_sec(){
	sec_select_which="sec";
	$("#sec_select_dialog").dialog("open");
}

function click_add_sec_base(){
	sec_select_which="base";
	$("#sec_select_dialog").dialog("open");
}

function sec_select_click_search(){
	var param = $("#search_param").form("getData");
	$('#sec_select_dg').datagrid('load', param);
}

function sec_select_click_save() {
	var selects = $('#sec_select_dg').datagrid('getSelections');

	var secGrid = $('#dg1');
	if (sec_select_which == "base") {
		var secGrid = $('#dg2');
	}
	addCompareSec(secGrid,selects);
	$("#sec_select_dialog").dialog("close");
	$('#sec_select_dg').datagrid('clearSelections');
}

function addCompareSec(secGrid, selects){
	var hasedData = secGrid.edatagrid("getData");
	for (var i = 0; i < selects.length; i++) {
		var exists = false;
		for(var j=0; j<hasedData.total;j++){
			if(selects[i].secId==hasedData.rows[j].secId){
				exists = true;
			}
		}
		if(!exists){
			secGrid.datagrid("appendRow", selects[i]);
		}
	}
}

function click_add_model(){
	$("#model_sec_select_dialog").dialog("open");
}

function changeAcount(newValue, oldValue) {
	$.ajax("./modelsec.json", {
			data : {acountId:newValue},
			mask : true,
			type : 'post',
			success : function(data){
				addCompareSec($('#dg1'),data.secList);
				addCompareSec($('#dg2'),data.baseList);
			}
		});
	$("#model_sec_select_dialog").dialog("close");
}

function click_analysis(){
	var analysisParam = $("#analysis_param").form("getData");
	analysisParam.analysisDateType = $("#analysisDateType").switchbutton("options").checked;
	
	var secIds="", secBaseIds="";
	var selData = $("#dg1").edatagrid("getData");
	if (selData.total < 1) {
		$.messager.show({
			title : '错误消息',
			timeout : 1000,
			msg : '选择证券',
			showType : 'fade',
			style : {
				right : '',
				bottom : ''
			}
		});
		return;
	}
	for(var i=0;i<selData.total;i++){
		secIds += ","+selData.rows[i].secId;
	}
	analysisParam.secIds=secIds;
	
	selData = $("#dg2").edatagrid("getData");
	if (selData.total < 1) {
		$.messager.show({
			title : '错误消息',
			timeout : 1000,
			msg : '选择比较证券',
			showType : 'fade',
			style : {
				right : '',
				bottom : ''
			}
		});
		return;
	}
	for(var i=0;i<selData.total;i++){
		secBaseIds += ","+selData.rows[i].secId;
	}
	analysisParam.secBaseIds=secBaseIds;
	
	$.ajax("./query.json", {
		data : analysisParam,
		mask : true,
		type : 'post',
		success : loadData
	});
}

function deleteSelected(datagridId){
	var secGrid = null;
	if(datagridId == 1){
		secGrid = $('#dg1');
	} else if(datagridId == 2){
		secGrid = $('#dg2');
	} else {
		return;
	}
	var row = secGrid.datagrid("getSelected");
	var index = secGrid.datagrid("getRowIndex", row);
	secGrid.datagrid("deleteRow", index);
}

function loadData(data){
	$("#dgResult1").edatagrid("loadData", data.risk);
	$("#dgResult2").edatagrid("loadData", data.regression);
	loadProfitChart(data);
	$(window).resize();
}
 
function loadProfitChart(data){
	var lines = data.dataLine;
	var legend = [];
	var series = [];
	for ( var key in lines) {
		legend.push(key);
		series.push({
			name : key,
			type : 'line',
			data : lines[key]
		});
	}
	// 指定图表的配置项和数据
	var option = {
		tooltip : {
			trigger : 'axis',
			axisPointer : {
				type : 'cross'
			},
			formatter : function(params) {
				var res = params[0].name;
				for (var i = 0, l = params.length; i < l; i++) {
					var a = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + '"></span>';
					res += '<br/>' + a + params[i].seriesName + ' : ' + params[i].value.toFixed(2) + "%";
				}
				return res;
			}
		},
		legend: {
			data:legend
		},
		dataZoom:{
			show: true,
			realtime: true,
			start: 0,
			end: 100
		},
		xAxis: {
		 	type: "category",
		    data: data.timeLine,
		    axisLabel:{formatter:function (value, index) {
		    	var date = new Date(value);
		    	var y = date.getFullYear();
				var m = date.getMonth()+1;
				var d = date.getDate();
				return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
		    }}
		},
	    yAxis: [{
			name: '收益%',
			type: 'value',
	            }
	        ],
		series:series
	};
	report1.setOption(option);
}


